﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using Flame.Reflection;
using Flame.Scintilla;
using Flame.Controls.Common;
using Flame.Dlr;

namespace Flame.Controls
{
    /// <summary>
    /// Manage Autocompletition
    /// </summary>
    internal class ScintillaNETAutoComplete : AAutoComplete
    {
        ScintillaNET.Scintilla _scintilla;
        ImageList _imageList;

        internal ScintillaNETAutoComplete(IExecEngines compilerManager, ICallTip calltip, ScintillaNET.Scintilla scintilla)
            : base(compilerManager, calltip)
        {
            _scintilla = scintilla;

            _imageList = new ImageList();
            ImageListIntellisense.FillIntellisense(_imageList);
            _scintilla.AutoComplete.RegisterImages(_imageList, Color.White);
            _scintilla.AutoComplete.DropRestOfWord = true;
            _scintilla.AutoComplete.IsCaseSensitive = false;
            _scintilla.AutoComplete.AutomaticLengthEntered = false;
            
        }

        protected override string textToCaret
        {
            get
            {
                int i;
                string text = _scintilla.GetCurrentLine(out i);
                return text.Substring(0, i);
            }
        }


        string getScintillaImageIndex(IntellisenseItemType t)
        {
            switch (t)
            {
                case IntellisenseItemType.Namespace:
                    return "?4";
                case IntellisenseItemType.Type:
                    return "?3";
                case IntellisenseItemType.Object:
                    return "";
                case IntellisenseItemType.Method:
                    return "?0";
                case IntellisenseItemType.Property:
                case IntellisenseItemType.Field:
                    return "?2";
                case IntellisenseItemType.Event:
                    return "?1";
            }
            return "";
        }

        protected override bool show(Intellisense.Result r)
        {
            if (r != null)
            {
                //_scintilla.AutoComplete.ListSeparator = '|';
                _scintilla.AutoComplete.List = (from x in r.List select x.Name + getScintillaImageIndex(x.Type)).ToList<string>();
                //_scintilla.AutoComplete.List.Sort();
                if (r.StartingWithCount > 0 || r.List.Count == 1)
                    _scintilla.AutoComplete.Show(r.PartialLength);
                else
                {
                    _scintilla.Caret.Position -= r.PartialLength;
                    _scintilla.Selection.Text = "";
                    _scintilla.AutoComplete.Show(0);
                }
                return true;
            }
            return false;
        }

        public override bool IsActive { get { return _scintilla.AutoComplete.IsActive; } }
    }
}
